package com.dilidili.config;

import com.dilidili.entity.User;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author sunshaoshan
 * @Title: MyJwtAccessTokenConverter
 * @ProjectName Springcloud-alibaba-A340
 * @Description: TODO
 * @date 2022/3/24 10:13
 */
public class MyJwtAccessTokenConverter extends JwtAccessTokenConverter {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        //jwt中封装uid对象
        User user =  (User)authentication.getUserAuthentication().getPrincipal();
        user.setUserPassword(null);
        Map<String, Object> additionalInformation = new LinkedHashMap<String, Object>();
        additionalInformation.put("uid",user.getUserId());
        DefaultOAuth2AccessToken defaultOAuth2AccessToken = (DefaultOAuth2AccessToken)accessToken;
        defaultOAuth2AccessToken.setAdditionalInformation(additionalInformation);
        DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken)super.enhance(accessToken, authentication);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("user",user);
        token.setAdditionalInformation(map);
        System.out.println(token);
        return token;
    }
}
